package com.dmooo.yxmmerchant.util;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.common.glide.GlideUtils;
import com.common.util.BasePopupWindow;
import com.common.util.LogUtil;
import com.common.util.ScreenUtil;
import com.dmooo.yxmmerchant.R;
import com.dmooo.yxmmerchant.common.Config;
import com.dmooo.yxmmerchant.domain.MallGoodsDetailBean;
import com.dmooo.yxmmerchant.domain.SelectSkubean;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;

import java.util.ArrayList;
import java.util.List;

public class SkuPopView extends BasePopupWindow {
    private List<MallGoodsDetailBean.GoodsMsg.SkuList> skuLists;
    private List<MallGoodsDetailBean.GoodsSku> goodsSkus;//商品现存的sku配置，用于选择后显示相应的图片
    private SkuListViewAdapter listViewAdapter;
    private Context context;
    private ListView skuListView;
    private String is_inqury="N";
    private int buyNum = 1, minBuyNum = 1;//默认购买数量 不能小于1
    TextView slectInfo, goods_name,txtPrice,txtKc,txt_goods_r_price;
    ImageView  goods_img;
    private String goodsName = "", goodsImg = "";
    private OnOkListener onOkListener;
    private List<SelectSkubean> skuList = new ArrayList<>();
    public SkuPopView(Context context, List<MallGoodsDetailBean.GoodsMsg.SkuList> skuLists, List<MallGoodsDetailBean.GoodsSku> goodsSkus, int minBuyNum, String goodsName, String goodsImg,OnOkListener onOkListener,String is_inqury) {
        super(ScreenUtil.getScreenWidth(context), ScreenUtil.getScreenHeight(context) * 3 / 4, R.layout.sku_pop, context);
        this.onOkListener=onOkListener;
        this.context = context;
        this.skuLists = skuLists;
        this.goodsSkus = goodsSkus;
        this.minBuyNum = minBuyNum;
        this.goodsName = goodsName;
        this.goodsImg = goodsImg;
        this.is_inqury=is_inqury;
        for (int i = 0; i <skuLists.size(); i++) {
            skuList.add(new SelectSkubean());
        }
        buyNum=Integer.valueOf(minBuyNum);
        listViewAdapter = new SkuListViewAdapter(context, skuLists);
        setAnimationStyle(R.style.MyAnimBottom);
        skuListView = getView(R.id.list_sku);
        txtKc=getView(R.id.txt_goods_kc);
        txt_goods_r_price=getView(R.id.txt_goods_r_price);
        txtPrice=getView(R.id.txt_goods_price);
        skuListView.setAdapter(listViewAdapter);
        goods_img = getView(R.id.goods_img);
        goods_name = getView(R.id.txt_goods_name);
        slectInfo = getView(R.id.txt_goods_sku);
        goods_name.setText(goodsName);
        GlideUtils.showImageViewToRound(context, context.getResources().getDrawable(R.mipmap.defaultpicture_classify), goodsImg, goods_img, ScreenUtil.dp2px((Activity) context, 2));
        initView();
        initData();
        setListener();
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }
    public void setAlpha(float f){
        WindowManager.LayoutParams lp = ((Activity)mContext).getWindow().getAttributes();
        lp.alpha =f; //0.0-1.0
        ((Activity)mContext).getWindow().setAttributes(lp);
    }
    @Override
    protected void setListener() {
        getView(R.id.img_close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                setAlpha(1);
            }
        });
        listViewAdapter.setsubClickListener(new SkuListViewAdapter.SubClickListener() {
            @Override
            public void OntopicClickListeners(View v, boolean detail, int posit, String str) {
                if (detail) {
                    skuList.get(posit).setString(skuLists.get(posit).attribute_id);
                    skuList.get(posit).setValue(str);
                } else {
                    skuList.get(posit).setString("");
                    skuList.get(posit).setValue("");
                }
                listViewAdapter.setSelectbeans(skuList);
                slectInfo.setText("已选择:");
                for (int i = 0; i < skuList.size(); i++) {
                    if (!"".equals(skuList.get(i).getString())) {
                        slectInfo.setText(slectInfo.getText() + skuList.get(i).getValue() + ",");
                    }
                }
                for (int i = 0; i < skuList.size(); i++) {
                    if (skuList.get(i).getString().equals("")) {
                        txtPrice.setText("Y".equals(is_inqury) ? "询价" : "");
                        txt_goods_r_price.setVisibility("Y".equals(is_inqury) ? View.GONE : View.VISIBLE);
//                        if(YzxApplication.getUserInfoBean()!=null&&!"1".equals(YzxApplication.getUserInfoBean().user_msg.group_id)){
//                            txt_goods_r_price.setVisibility(View.VISIBLE);
//                            txt_goods_r_price.setText("");
//                        }else{
                        txt_goods_r_price.setVisibility(View.GONE);
//                        }
                        txtKc.setText("库存: 0");
                        return;
                    }
                }
                JsonArray s = new JsonArray();
                JsonObject object1;
                for (int i = 0; i < skuList.size(); i++) {
                    object1 = new JsonObject();
                    object1.addProperty("attribute_id", Integer.valueOf(skuList.get(i).getString()));
                    object1.addProperty("value", skuList.get(i).getValue());
                    s.add(object1);
                }
                for (int j = 0; j < goodsSkus.size(); j++) {
                    LogUtil.d("sdfsdf", goodsSkus.get(j).sku + " " + s.toString());
                    if (goodsSkus.get(j).sku.contains(s.toString())) {
                        txtPrice.setText("Y".equals(is_inqury) ? "询价" : Config.RMB + goodsSkus.get(j).price);
                        txt_goods_r_price.setVisibility("Y".equals(is_inqury) ? View.GONE : View.VISIBLE);
//                        if(YzxApplication.getUserInfoBean()!=null&&!"1".equals(YzxApplication.getUserInfoBean().user_msg.group_id)){
//                            txt_goods_r_price.setVisibility(View.VISIBLE);
                        txt_goods_r_price.setText("智享价: " + Config.RMB + goodsSkus.get(j).r_price);
//                        }else{
//                            txt_goods_r_price.setVisibility(View.GONE);
                    }
                    txtKc.setText("库存: " + goodsSkus.get(j).inventory);
//                        GlideUtils.showImageViewToCircle(mContext,mContext.getResources().getDrawable(R.mipmap.defaultpicture_classify),Config.APP_INTERFACE_BASE_URL+goodsSkus.get(j).img,goods_img);
                    break;
                }
            }
        });
    }

    public int getBuyNum() {
        return buyNum;
    }
    public interface OnOkListener{
        void onOk();
    }
    public void setBuyNum(int buyNum) {
        this.buyNum = buyNum;
    }
    public String getPrice(){
        return "Y".equals(is_inqury)?"0.00":txtPrice.getText().toString().replace(Config.RMB,"");
    }
    public List<SelectSkubean> getSkuList() {
        return skuList;
    }

    public void setSkuList(List<SelectSkubean> skuList) {
        this.skuList = skuList;
    }
}
